﻿$(document).ready(function () {

    $("p.insert_provider").click(function () {


        var name = $("form input[name='name']").val();
        var address = $("form input[name='address']").val();
        var phone = $("form input[name='phone']").val();
        var email = $("form input[name='email']").val();
        var id_cart = $("form input[name='id_card']").val();
        if (validateEmail(email) && validatePhone(phone)) {
            CallAjax(name, address, id_cart, phone, email);
        } else {
            alert("bạn nhập email hoặc số điện thoại không đúng");
        }

        
    });

    $("p.save").click(function () {
        var name = $("#form input[name='name']").val();
        var address = $("#form input[name='address']").val();
        var phone = $("#form input[name='phone']").val();
        var email = $("#form input[name='email']").val();
        var id_cart = $("#form input[name='id_card']").val();
        var id = $("#form input[name='id']").val();

        if (validateEmail(email) && validatePhone(phone)) {
            Save(name, address, id_cart, phone, email, id);
        } else {
            alert("bạn nhập email hoặc số điện thoại không đúng");
        }
        
    });
});
function CallAjax(name, address, id_card, phone, email) {
    $.post("/provider/insert", { name: name, address: address, id_cart: id_card, phone: phone, email: email },
       function (e) {
           $("#list_group").append(e);
       });
}
function CallAjaxDelete(param, element) {
    $.post("/Product/delete", { id: param },
       function (e) {
           if (e.status = "success") {
               $(element).remove();

           }

       });
}
function Save(name, address, id_card, phone, email, id) {

    $.post("/Provider/Save_provider", { name: name, address: address, id_cart: id_card, phone: phone, email: email, id: id },
       function (e) {
           if (!e.hasOwnProperty('status')) {
               // $(element).parent("td:eq(1)").text(name);
               $(element).parent("td").parent("tr").children("td[name='name']").text(name);
               $(element).parent("td").parent("tr").children("td[name='address']").text(address);
               $(element).parent("td").parent("tr").children("td[name='id_card']").text(id_card);
               $(element).parent("td").parent("tr").children("td[name='phone']").text(phone);
               $(element).parent("td").parent("tr").children("td[name='email']").text(email);
               $(element).attr("name", name);
               $(element).attr("phone", phone);
               $(element).attr("email", email);
               $(element).attr("address", address);
               $(element).attr("id_card", id_card);
               


               $("#form").dialog("close");
           } else {
               alert(e.error);
           }


       });
}
function deleteclick(element) {
    var id = $(element).attr("index");
    CallAjaxDelete(id, $(element).parent("td").parent("tr"));
}
function edit_group(item) {
    Dochange(item);
}
function Dochange(item) {
    $("form input[name='name']").prop("disabled", true);
    $("form input[name='address']").prop("disabled", true);
    $("form input[name='phone']").prop("disabled", true);
    $("form input[name='email']").prop("disabled", true);
    $("form input[name='id_card']").prop("disabled", true);
    $("p.insert_provider").hide();
    $("#form").dialog({
        close: function (event, ui) {

            $("form input[name='name']").prop("disabled", false);
            $("form input[name='address']").prop("disabled", false);
            $("form input[name='phone']").prop("disabled", false);
            $("form input[name='email']").prop("disabled", false);
            $("form input[name='id_card']").prop("disabled", false);
            $("p.insert_provider").show();

        }
    });
    var id = $(item).attr("index");
    var name = $(item).attr("name");
    var phone = $(item).attr("phone");
    var address = $(item).attr("address");
    var email = $(item).attr("email");
    var id_card = $(item).attr("id_card");

    $("#form input[name='name']").val(name);
    $("#form input[name='address']").val(address);
    $("#form input[name='phone']").val(phone);
    $("#form input[name='email']").val(email);
    $("#form input[name='id_card']").val(id_card);
    $("#form input[name='id']").val(id);
    element = $(item);
}
